package frame;

import Homework.Commodity;
import Homework.UseImp;
import Practice0915.FilPress;
import com.lovo.netCRM.component.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;


/*
主窗体
 */
public class MainFrame extends JFrame {

    /**
     * 创建表格组件，第一个参数为加入的容器，第二个参数为表头列表，第三个为每个表头对应的属性列表
     * 第四个参数代表实体唯一标识的属性名
     */


    private LovoTable table = new LovoTable(this,new String[]{"编号","商品名","单价","厂家","生产日期"},
            new String[]{"code","name","price","factory","date"},"code");
    private UseImp imp = new UseImp();

    private LovoTxt searchTxt = new LovoTxt("商品名",50,5,this);

    private LovoComboBox<String> sortBox = new LovoComboBox<>("请选择排序列",new String[]{"单价","日期"},50,250,this);

    public MainFrame(){
        this.setLayout(null);

        this.init();

        this.setSize(600,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){

        /**设置表格大小和位置*/
        this.table.setSizeAndLocation(50,30,400,150);
        find();

        LovoButton searchButton = new LovoButton("查找",300,5,this);
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                search();
            }
        });

        LovoButton addButton = new LovoButton("添加",50,200,this);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addCommodity();
            }
        });

        LovoButton delButton = new LovoButton("删除",150,200,this);
        delButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                delCommodity();
            }
        });

        LovoButton upDateButton = new LovoButton("修改",250,200,this);
        upDateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String x = table.getKeyByString();
                if (x==null){
                    JOptionPane.showMessageDialog(null,"请选中行");
                    return;
                }

                int code = Integer.parseInt(x);
                dispose();
                new UpdateFrame(code);
            }
        });

        LovoButton sortButton = new LovoButton("排序",300,250,this);
        sortButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sort();
            }
        });
    }


    private void sort(){
        imp.rank(sortBox.getItem());
        this.find();
    }

    private void delCommodity(){
        String x = table.getKeyByString();
        if (x==null){
            JOptionPane.showMessageDialog(null,"请选中行");
            return;
        }

        int code = Integer.parseInt(x);
        List<Commodity> l = imp.delete(code);
        table.updateLovoTable(l);
    }

    private void addCommodity(){
        this.dispose();
        new AddFrame();


    }

    private void find(){
        List<Commodity> list = imp.show();
        table.updateLovoTable(list);
    }


    private void search(){
        List<Commodity> list = imp.vSearch(searchTxt.getText());
        table.updateLovoTable(list);
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}
